package com.wonders.module.auth.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.common.constant.CommonConstant;
import com.wonders.framework.dao.impl.BaseDaoImpl;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.entity.vo.Parameter;
import com.wonders.framework.util.DateUtil;
import com.wonders.module.auth.dao.UserDao;
import com.wonders.module.auth.entity.bo.User;
import com.wonders.module.auth.entity.vo.UserRoleVo;
import com.wonders.module.auth.entity.vo.UserVo;

/**
 * 用户信息数据层实现
 * 
 * @author WuBin
 * 
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

	public User getUser(UserVo userVo) {
		Criteria criteria = super.getCurrentSession()
				.createCriteria(User.class);
		criteria.add(Restrictions.eq("loginName", userVo.getLoginName()));
		criteria.add(Restrictions.eq("password", userVo.getPassword()));
		criteria.add(Restrictions.eq("enable", CommonConstant.Status.ENABLED));
		return super.findByCriteria(criteria, User.class);
	}

	public Page query(UserVo userVo, Page page) {
		Criteria criteria = super.getCurrentSession()
				.createCriteria(User.class);
		if (StringUtils.isNotBlank(userVo.getLoginName())) {
			criteria.add(Restrictions.like("loginName",
					"%" + userVo.getLoginName() + "%"));
		}
		if (StringUtils.isNotBlank(userVo.getName())) {
			criteria.add(Restrictions.like("name", "%" + userVo.getName() + "%"));
		}
		if (StringUtils.isNotBlank(userVo.getTel())) {
			criteria.add(Restrictions.like("tel", "%" + userVo.getTel() + "%"));
		}
		if (StringUtils.isNotBlank(userVo.getEmail())) {
			criteria.add(Restrictions.like("email", "%" + userVo.getEmail()
					+ "%"));
		}
		if (userVo.getCreateDate() != null) {
			criteria.add(Restrictions.between("createDate",
					userVo.getCreateDate(),
					DateUtil.getDateByDays(userVo.getCreateDate(), 1)));
		}
		criteria.add(Restrictions.eq("enable", userVo.getEnable()));
		criteria.addOrder(Order.desc("id"));
		return super.pageByCriteria(criteria, page);
	}

	public void remove(List<Long> keys) {
		String hql = "update User u set u.enable = :enable where u.id in (:keys)";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("enable", CommonConstant.Status.DISABLED));
		args.add(new Parameter("keys", keys));
		super.excuteHql(hql, args);
	}

	public void editPassword(List<Long> keys, String password) {
		String hql = "update User u set u.password = :password where u.id in (:keys)";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("password", password));
		args.add(new Parameter("keys", keys));
		super.excuteHql(hql, args);
	}

	public List<Map<String, Object>> isExist(UserVo userVo) {
		StringBuffer sql = new StringBuffer("select key, count from (");
		List<Parameter> args = new ArrayList<Parameter>();
		if (StringUtils.isNotBlank(userVo.getLoginName())) {
			sql.append("select '登录名' as key, count(1) as count, '1' as seq from au_user u where u.login_name = :loginName #if");
			args.add(new Parameter("loginName", userVo.getLoginName()));
		}
		if (StringUtils.isNotBlank(userVo.getTel())) {
			if (sql.length() > 0) {
				sql.append("union ");
			}
			sql.append("select '手机号' as key, count(1) as count, '2' as seq from au_user u where u.tel = :tel #if");
			args.add(new Parameter("tel", userVo.getTel()));
		}
		if (StringUtils.isNotBlank(userVo.getEmail())) {
			if (sql.length() > 0) {
				sql.append("union ");
			}
			sql.append("select '邮箱' as key, count(1) as count, '3' as seq from au_user u where u.email = :email #if");
			args.add(new Parameter("email", userVo.getEmail()));
		}
		sql.append(") where count > 0 order by seq");
		if (userVo.getId() != null) {
			args.add(new Parameter("id", userVo.getId()));
			return super.listBySql(
					sql.toString().replaceAll("#if", "and id <> :id "), args);
		}
		return super.listBySql(sql.toString().replaceAll("#if", ""), args);
	}

	public Page queryRoleUser(UserRoleVo userRoleVo, Page page) {
		String hql = null;
		if (!"notIn".equals(userRoleVo.getQueryType())) {
			hql = "select u from User u, UserRole ur where u.id = ur.userId and ur.roleId = :roleId";
		} else {
			hql = "select u from User u where not exists (select '1' from UserRole ur where u.id = ur.userId and ur.roleId = :roleId)";
		}
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("roleId", userRoleVo.getRoleId()));
		return super.pageByHql(hql, args, page);
	}

}
